Introduce new fdt helper to read string properties
authorAntonio Nino Diaz <[email protected]>
Tue, 26 Jun 2018 09:34:07 +0000 (10:34 +0100)
committerAntonio Nino Diaz <[email protected]>
Fri, 2 Nov 2018 14:55:16 +0000 (14:55 +0000)
Introduced fdtw_read_string() to read string properties.

Change-Id: I854eef0390632cf2eaddd2dce60cdb98c117de43
Signed-off-by: Antonio Nino Diaz <[email protected]>
common/fdt_wrappers.c
include/common/fdt_wrappers.h

index 1a726a8aaaba151b8a7b4ba682a2425fddd6ce86..1715a6f0e124f6cd09d520302872d44c4ee8a4f1 100644 (file)
@@ -10,6 +10,7 @@
 #include <debug.h>
 #include <fdt_wrappers.h>
 #include <libfdt.h>
+#include <string.h>
 
 /*
  * Read cells from a given property of the given node. At most 2 cells of the
@@ -61,6 +62,38 @@ int fdtw_read_cells(const void *dtb, int node, const char *prop,
        return 0;
 }
 
+/*
+ * Read string from a given property of the given node. Up to 'size - 1'
+ * characters are read, and a NUL terminator is added. Returns 0 on success,
+ * and -1 upon error.
+ */
+int fdtw_read_string(const void *dtb, int node, const char *prop,
+               char *str, size_t size)
+{
+       const char *ptr;
+       size_t len;
+
+       assert(dtb != NULL);
+       assert(node >= 0);
+       assert(prop != NULL);
+       assert(str != NULL);
+       assert(size > 0U);
+
+       ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL);
+       if (ptr == NULL) {
+               WARN("Couldn't find property %s in dtb\n", prop);
+               return -1;
+       }
+
+       len = strlcpy(str, ptr, size);
+       if (len >= size) {
+               WARN("String of property %s in dtb has been truncated\n", prop);
+               return -1;
+       }
+
+       return 0;
+}
+
 /*
  * Write cells in place to a given property of the given node. At most 2 cells
  * of the property are written. Returns 0 on success, and -1 upon error.
index 3eae944e5bb8fca29047651eafb4b9eb94c04a54..a0fe6b753098405b8738f520c63dbe6b7ae7deb0 100644 (file)
@@ -14,6 +14,9 @@
 
 int fdtw_read_cells(const void *dtb, int node, const char *prop,
                unsigned int cells, void *value);
+int fdtw_read_string(const void *dtb, int node, const char *prop,
+               char *str, size_t size);
 int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
                unsigned int cells, void *value);
+
 #endif /* __FDT_WRAPPERS__ */